var columns = [{
    checkbox: true
},
{
    field: "id",
    title: "编号"
},
{
    field: "name",
    title: "资源名称"
},
{
    field: "parentName",
    title: "上级名称"
},
{
    field: "sourceKey",
    title: "标识"
},
{
    field: "type",
    title: "类型",
    formatter: function(value, row, index) {
        switch (value) {
        case 0:
            return "目录";
        case 1:
            return "菜单";
        case 2:
            return "按钮";
        default:
            break;
        }
    }
},
{
    field: "icon",
    title: "图标",
    formatter: function(value, row, index) {
        return '<i class="' + value + '"></i>';
    }
},
{
    field: "sourceUrl",
    title: "资源URL",
    formatter: function(value, row, index) {
        return '<i class="' + value + '"></i>';
    }
},
{
    field: "createTime",
    title: "创建时间",
    formatter: function(value, row, index) {
        return value != null && value != "" ? new Date(value).toISOString().replace(/T/g, ' ').replace(/\.[\d]{3}Z/, '') : null;
    }
},
{
    field: "updateTime",
    title: "更新时间",
    formatter: function(value, row, index) {
        return value != null && value != "" ? new Date(value).toISOString().replace(/T/g, ' ').replace(/\.[\d]{3}Z/, '') : null;
    }
}];


function queryParams(params) {
    var param = {
        pageNumber: params.pageNumber,
        pageSize: params.pageSize,
        keyword: $('.search input').val()
    };
    return param;
}

//通过table id获取需要初始化的table
var resource_table = $('#resource_table');

//初始化的table
initTable(resource_table, $('#resource_toolbar'), 'resource/list', columns, queryParams, 'zh-CN', true);

//给搜索框添加提示
$('.search input').attr("placeholder","搜索资源名称");

function resetPassword() {
    var ids = getSelections(resource_table, 'id');
    if (ids.length == 1) {
        loadPage('resource/' + ids.toString() + '/toRestPassword');
    } else {
        layer.msg("你没有选择行或选择了多行数据", {
            icon: 0
        });
    }
}

function delResource(id) {
    del("resource/" + id + "/delete", search);
}

function btnDel() {
    var ids = getSelections(resource_table, 'id');
    console.log(ids);
    if (ids.length == 0) {
        layer.msg("请选择要删除的记录", {
            icon: 0
        });
    } else {
        layer.confirm('确定删除所选记录？', {
            icon: 3
        },
        function(index) {
        	
        	$.post('resource/delete', {
        	    "json": JSON.stringify(ids)
        	},
        	function(json) {
        	    if (json.status == 0) {
        	        refreshDelTable(resource_table, ids.length);
        	    }
        	    layer.msg(json.message);
        	});
        	
            layer.close(index);
        });
    }
}

function btnEdit() {
    var ids = getSelections(resource_table, 'id');
    if (ids.length == 1) {
        loadPage('resource/' + ids.toString() + '/select');
    } else {
        layer.msg("你没有选择行或选择了多行数据", {
            icon: 0
        });
    }
}